/*
 * Javlov - a Java toolkit for reinforcement learning with multi-agent support.
 * 
 * Copyright (c) 2009 Matthijs Snel
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package net.javlov.world.phys2d;

import java.awt.Color;
import java.awt.Shape;
import java.awt.geom.Point2D;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import net.javlov.world.Body;
import net.javlov.world.Property;
import net.phys2d.math.ROVector2f;
import net.phys2d.raw.shapes.DynamicShape;

public class Phys2DBody extends net.phys2d.raw.Body implements Body  {
	
	protected Map<String, Property> properties;
	
	private boolean isStatic;
	private int type;
	private double reward;
	
	public Phys2DBody(DynamicShape shape, float m, boolean staticBody) {
		super(shape, m);
		setType(0);
		setStatic(staticBody);
		properties = new HashMap<String, Property>();
	}
	
	public Phys2DBody(net.phys2d.raw.shapes.Shape shape, float m, boolean staticBody) {
		this("Unnamed", shape, m, staticBody);
	}
	
	public Phys2DBody(String name, net.phys2d.raw.shapes.Shape shape, float m, boolean staticBody) {
		super(name, shape, m);
		setType(0);
		setStatic(staticBody);
	}
	
	@Override
	public Shape getFigure() {
		return getShape();
	}

	@Override
	public Point2D getLocation() {
		ROVector2f pos = getPosition();
		return new Point2D.Double(pos.getX(), pos.getY());
	}

	public void setLocation(double x, double y) {
		setPosition((float)x, (float)y);
	}
	
	@Override
	public double getBearing() {
		return getRotation();
	}

	@Override
	public double getX() {
		return getPosition().getX();
	}

	@Override
	public double getY() {
		return getPosition().getY();
	}

	@Override
	public Point2D getSpeed() {
		ROVector2f v = getVelocity();
		return new Point2D.Double(v.getX(), v.getY());
	}
	
	@Override
	public boolean isStatic() {
		return isStatic;
	}
	
	protected void setStatic(boolean staticBody) {
		isStatic = staticBody;
		if ( isStatic ) {
			setMass(net.phys2d.raw.Body.INFINITE_MASS);
			setRotatable(false);
			setMoveable(false);
			setIsResting(true);
		}
	}
	
	public int getType() {
		return type;
	}
	
	public void setType( int t ) {
		type = t;
	}
	
	@Override
	public void addProperty( Property p ) {
		properties.put(p.getName(), p);
	}
	
	@Override
	public Collection<Property> getProperties() {
		return Collections.unmodifiableCollection(properties.values());
	}
	
	@Override
	public Property getProperty( String name ) {
		return properties.get(name);
	}
	
	@Override
	public double getPropertyValue( String name ) {
		return properties.get(name).getValue();
	}
	
	@Override
	public void updateProperty(String name, double delta) {
		Property p = properties.get(name);
		p.update(delta);
	}

	@Override
	public double getReward() {
		return reward;
	}

	@Override
	public void setReward(double r) {
		reward = r;
	}
}
